package com.chengdu.chun.controller;

import com.chengdu.chun.po.Classes;
import com.chengdu.chun.po.Page;
import com.chengdu.chun.po.Room;
import com.chengdu.chun.po.User;
import com.chengdu.chun.service.RoomService;
import com.chengdu.chun.util.Sys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;
    @Autowired
    private HttpSession session;

    @GetMapping("/roomsbypage/{page}/{num}.html")
    public String roomsbypage(Model model, @PathVariable("page") Integer page, @PathVariable("num") Integer num) {
        if (page == null || page <= 0) page = 1;
        if (num == null || num <= 0) page = Sys.PAGE_THREE;
        Page<Room> roomPage = roomService.getPageByroom(page, num);

        model.addAttribute("roomPage", roomPage);
        return "rooms";
    }

    @GetMapping("/roombyname")
    public String roombyname(Model model, String roomname) {
        String rname = roomname.length() == 0 ? null : roomname;
        List<Room> roomList = roomService.getRoomByName(rname);
        Page<Room> roomPage = new Page<>(1, roomList.size(), roomList.size());
        roomPage.setDatalist(roomList);
        model.addAttribute("roomPage",roomPage);
        return "rooms";
    }
}
